/**
 * 
 */
package com.gzceb.zhgjj.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.gzceb.zhgjj.mina.MinaConstants;

/**
 * <p>
 * 工具类。
 * </p>
 * 
 * @date 2012-10-18下午4:29:58
 * @author liqing
 * @description 工具大杂烩
 */
public class Utils {

	private static long pkId; // 唯一Long型主码

	public static synchronized long getPkId() {
		long lTmp = System.currentTimeMillis();
		if (pkId < lTmp) {
			pkId = lTmp;
		} else {
			pkId++;
		}
		return pkId;
	}

	/**
	 * 获取当前日期格式化字符串
	 */
	public static String getCurrDateStr(String formatString){
		Calendar calendar=Calendar.getInstance();
		SimpleDateFormat format=new SimpleDateFormat(formatString);
		String dateStr=format.format(calendar.getTime());
		return dateStr;
	}
	public static String getCurrDateStr19(){
		return getCurrDateStr("yyyy-MM-dd HH:mm:ss");
	}
	public static String getCurrDateStr19Str(){
		return getCurrDateStr("yyyyMMddHHmmss");
	}
	
	/**
	 * 获取 Timestamp 类型的时间
	 * @param dateformat String
	 * @return  Timestamp
	 * 
	 * String
	 */
	public static Timestamp getCurrDateTimeStamp(){
		DateFormat dateFormat;
	    dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss", Locale.ENGLISH);//设定格式
		  //dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss", Locale.ENGLISH);
		  dateFormat.setLenient(false);
		  Date timeDate  = new Date();
		  try{
			  timeDate = dateFormat.parse(Utils.getCurrDateStr19Str());//util类型
		  }catch (ParseException pe){
			  
		  }
		  java.sql.Timestamp dateTime = new java.sql.Timestamp(timeDate.getTime());//Timestamp类型,timeDate.getTime()返回一个long型
		  return dateTime;

	}
	
	/**
	 * 从字符串获取DATE格式的日期
	 * @param String yyyyMMdd
	 * @return DATE
	 */
	public static Date getDate(String date){
		DateFormat dateFormat;
	    dateFormat = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);//设定格式
	    
	    Date timeDate  = new Date();
	    try{
			  timeDate = dateFormat.parse(date);
		  }catch (ParseException pe){
			  
		  }
	    return timeDate;
	}
	
	/**
	 * 从TTF001的文件名头字节获取文件类型
	 */
	public static String getFileType(String fileName){
		String name = fileName.substring(0, 2);
		if("LA".equals(name)){
			return MinaConstants.RSP_APRV.getValue();
		}else if("LB".equals(name)){
			return MinaConstants.RSP_AUTH.getValue();
		}else if("LC".equals(name)){
			return MinaConstants.RSP_AUTHCNL.getValue();
		}else{
			return MinaConstants.RSP_ACCTOPN.getValue();
		}
	}
	
	/**
	 * 数字转换为字符串，按设定长度左补零，补齐位数
	 * @param length int, num int
	 * @return String
	 * 
	 */
	public static String intToString(int num, int length){
		String strNum = Integer.toString(num);
		if(strNum.length() >= length){
			return strNum; // 如果给定的字符串长度比实际数小，那就返回实际数
		}else{
			int tmpLength = strNum.length();
			for(int i=0;i<(length - tmpLength);i++){
				strNum = "0" + strNum;
			}
			return strNum;
		}
		
	}
	
	/**
	 * 将表示数字的字符串，去除左侧的零值
	 * @param num String
	 * @return String
	 */
	public static String stringToNoZero(String num){
		int tmp = Integer.parseInt(num);
		String str = Integer.toString(tmp);
		return str;
	}
	
	/**
	 * 从绝对路径的字符串中获取最后的文件名,兼容UNIX和WINDOWS目录分隔符
	 * @param absPath String
	 * @return fileName String
	 * 
	 */
	public static String getFileName(String absPath){
		String fileName="";
		String  regex = "/";
		System.out.println(absPath.matches(regex));
		absPath = absPath.replaceAll(regex, Matcher.quoteReplacement(System.getProperty("file.separator")));
		
		for(int i = 0; i< absPath.length(); i = absPath.indexOf(System.getProperty("file.separator"))+1 ){
			absPath = absPath.substring(i);
			if(-1 == absPath.indexOf(System.getProperty("file.separator")) ){
				fileName = absPath;
				break;
			}
		}
		return fileName;
	}
	
	/*
	 * 程序入口：测试一下本类的各个方法是否成功
	 */
	public static void main(String[] args) {

		/*
		System.out.println("@@@@@@2  getPkId() = " + getPkId());
		System.out.println(getCurrDateStr("yyyyMMdd"));
		System.out.println(getCurrDateTimeStamp());
		*/
		/*String str = "DJ010500201202150001";
		String str1 = System.getProperty("file.separator")
				+"dqzxt" + System.getProperty("file.separator")+"file"
				+ System.getProperty("file.separator")+ "jjzx"
				+ System.getProperty("file.separator")+ "DJ010500201202150001";
		String name = getFileName(str);
		*/
		String str = "20121218";
		Date d = getDate(str);
	}

}
